package server.main.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import server.main.mapper.PermissionMapper;
import server.main.mapper.RoleMapper;
import server.main.mapper.RolePermissionMapper;
import server.main.pojo.Permission;
import server.main.pojo.Role;
import server.main.service.RoleService;

import java.util.List;

/**
 * @auther MingHsZzz
 * @Date 2022/10/4  15:54
 * @project main
 * @description
 */

@Service("RoleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    RoleMapper roleMapper;



    @Autowired
    PermissionMapper permissionMapper;

    @Autowired
    RolePermissionMapper rolePermissionMapper;




    @Override
    public Page<Role> getRolesByPages(String name, Integer pageNum,Integer pageSize) {


        //        Result.success(roleService.page(new Page<>(pageNum, pageSize), Wrappers.<Role>lambdaQuery().like(Role::getName, name)));

        Page<Role> page = page(new Page<>(pageNum, pageSize));
        for (Role record : page.getRecords()) {
            List<Permission> permissions = roleMapper.Role2Permission(record.getId());

            if(permissions.get(0) != null){
                record.setPermissions(permissions);
            }
//            System.out.println("diyige");
        }

        System.out.println("getRolesByPages" + page.getRecords().toString());
        return page;

    }

    @Override
    public boolean saveRole(Role role) {
        System.out.println(role);
        return save(role);
    }


    @Override
    public boolean deleteRoleById(String roleid) {

        rolePermissionMapper.deleterole(roleid);

        roleMapper.deleteById(roleid);

        return true;
    }

    @Override
    public boolean updaterole(Role role){

        roleMapper.updateById(role);

        return true;
    }

    @Override
    public List<Role> getAllRoles() {
        List<Role> all = roleMapper.getAllr();
        System.out.println(all);

        return all;
    }

}
